package com.rjt.spider;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

import javax.imageio.stream.FileImageOutputStream;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.rjt.article.vo.ArticleVo;
import com.rjt.common.util.TextUtil;

import blade.kit.http.HttpRequest;


public class SpiderUtil {
	public static void main(String[] args) {
		System.out.println(initArticleVo("https://mp.weixin.qq.com/s/-1N6jWharXP5DvNlBFTnEA","C:\\opt\\upload","http://127.0.0.1"));
		
	}
	public static ArticleVo initArticleVo(String url,String path,String hostUrl) {
		// TODO Auto-generated method stub
		ArticleVo articleVo=new ArticleVo();
		try {
		Document document;
		if(url.indexOf("http")==0) {
			document= Jsoup.connect(url).timeout(10000).get();
		}else {
			document = Jsoup.parse(url);
		}
		
		String author=document.getElementsByClass("profile_nickname").text();
		
		String content=document.getElementById("js_content").html();
		articleVo.setAuthor(author);
		articleVo.setContent(handImg(content,path,hostUrl));
		
		Elements scripcts = document.getElementsByTag("script");
		for (Element element : scripcts) {
			/*取得JS变量数组*/
			String[] data = element.data().toString().split("var");
			/*取得单个JS变量*/
			for(String variable : data){
				/*过滤variable为空的数据*/
				if(variable.contains("=")){
					/*取到满足条件的JS变量*/
					if(variable.contains("msg_title") || variable.contains("msg_desc") 
							|| variable.contains("msg_cdn_url")){
						
						String[]  kvp = variable.split("=");
						if("msg_title".equals(kvp[0].trim())) {
							articleVo.setTitle(kvp[1].replace("\"", "").replaceAll(";", ""));
						}
						if("msg_desc".equals(kvp[0].trim())) {
							articleVo.setDescribe(kvp[1].replace("\"", "").replaceAll(";", ""));
						}
						if("msg_cdn_url".equals(kvp[0].trim())) {
							String fileName=Calendar.getInstance().getTimeInMillis()+".jpg";
							String imgUrl=kvp[1].replaceAll("\"", "");
							articleVo.setImgUrl(saveImg(imgUrl, path,fileName,hostUrl));
						}
					}
				}
			}
		}
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return articleVo;
	}
	/**
	 * 获取img的地址
	 * @param content
	 *  既可以是url，也可以是html
	 */
	public static String handImg(String content,String path,String hostUrl) {
		try {
			Document document;
			if(content.indexOf("http")==0) {
				document= Jsoup.connect(content).get();
			}else {
				document = Jsoup.parse(content);
			}
		    Elements elements = document.select("img[data-src]");  
		    for (Element element : elements) {
		    	  
		    	String url=element.attr("data-src");
		    	if(url.startsWith("https://mmbiz.qpic.cn")) {
		    		//获取类型
		    		String type=TextUtil.getSubString(url, "wx_fmt=", "&");
			    	//替换图片
			    	String fileName=Calendar.getInstance().getTimeInMillis()+"."+type;
			    	content=content.replace(url, saveImg(url,path,fileName,hostUrl)).replace("data-src", "src");
		    	}
			}
		    Elements srcElements = document.select("img[src]");  
		    for (Element element : srcElements) {
		    	String url=element.attr("src");
		    	if(url.startsWith("https://mmbiz.qpic.cn")) {
		    		//获取类型
			    	String type=TextUtil.getSubString(url, "wx_fmt=", "&");
			    	//替换图片
			    	String fileName=Calendar.getInstance().getTimeInMillis()+"."+type;
			    	
			    	content=content.replace(url, saveImg(url,path,fileName,hostUrl));
		    	}
			}
		    Elements sectionElements = document.select("section[style]");  
		    for (Element element : sectionElements) {
		    	String style=element.attr("style");
		    	int urlStart = style.indexOf("https://mmbiz.qpic.cn");
		    	if(urlStart!=-1) {
		    		String url=TextUtil.getSubString(style, "background-image: url(\"", "\"");
		    		//获取类型
		    		String type=TextUtil.getSubString(url, "wx_fmt=", "&");
			    	//替换图片
			    	String fileName=Calendar.getInstance().getTimeInMillis()+"."+type;
			    	
			    	content=content.replace(url, saveImg(url,path,fileName,hostUrl));
		    	}
		    	
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println(e.getMessage());
		}
		return content;
	}
	
	
	
	private static String saveImg(String url,String path,String fileName,String hostUrl) {
		String newUrl="";
		HttpRequest httpRequest = HttpRequest.get(url);
		byte[] bytes = httpRequest.bytes();
		try {
			byte2image(bytes,path+fileName);
			newUrl=hostUrl+"/image/"+fileName;
		} catch (Exception e) {
			// TODO: handle exception
			newUrl="";
		}
		return newUrl;
	}
	//byte数组到图片
	private static void byte2image(byte[] data,String path) throws Exception{
	    if(data.length<3||path.equals("")) return;
	    FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
	    imageOutput.write(data, 0, data.length);
	    imageOutput.close();
	  }
}
